<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="是否火灾报警" prop="fire">
        <el-select
          v-model="queryParams.fire"
          placeholder="请选择是否火灾报警"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in dict.type.alarm_yes_or_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否倾倒" prop="fall">
        <el-select
          v-model="queryParams.fall"
          placeholder="请选择是否倾倒"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in dict.type.alarm_yes_or_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['alarm:facilityRealtime:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['alarm:facilityRealtime:edit']"
          >修改</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['alarm:facilityRealtime:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['alarm:facilityRealtime:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="facilityRealtimeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="left" />
      <el-table-column type="index" label="序号" width="60" align="left" />
      <el-table-column
        prop="facilityName"
        label="设施"
        width="160"
        align="left"
      />
      <el-table-column
        label="数据时间"
        align="left"
        prop="dataTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dataTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否在线" align="left" prop="onlineStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.dev_online_status"
            :value="scope.row.onlineStatus"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="信号强度" align="left" prop="rssi" /> -->
      <!-- <el-table-column label="信噪比" align="left" prop="snr" />
      <el-table-column label="电池电压(伏特)" align="left" prop="voltage" /> -->
      <el-table-column label="距离值(厘米)" align="left" prop="distance" />
      <!-- <el-table-column label="信号接收功率" align="left" prop="rsrp" /> -->
      <!-- <el-table-column label="温度" align="left" prop="temperature" /> -->
      <!-- <el-table-column label="信号强度级别" align="left" prop="sign" /> -->
      <el-table-column label="满溢状态" align="left" prop="full">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.alarm_yes_or_no"
            :value="scope.row.full"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="帧计数" align="left" prop="frameCount" /> -->
      <el-table-column label="是否火灾报警" align="left" prop="fire">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.alarm_yes_or_no"
            :value="scope.row.fire"
          />
        </template>
      </el-table-column>
      <el-table-column label="是否倾倒" align="left" prop="fall">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.alarm_yes_or_no"
            :value="scope.row.fall"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="电池状态"
        align="left"
        prop="batteryQuantityStatus"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.alarm_yes_or_no"
            :value="scope.row.batteryQuantityStatus"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="角度；设备倾斜的相对角度"
        align="left"
        prop="angle"
      /> -->
      <el-table-column
        label="操作"
        align="left"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['alarm:facilityRealtime:edit']"
            >修改</el-button
          > -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['alarm:facilityRealtime:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

    <!-- 添加或修改满溢设备实时数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备id" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备id" />
        </el-form-item>
        <el-form-item label="数据时间" prop="dataTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.dataTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择数据时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否在线 1=在线，2=离线" prop="onlineStatus">
          <el-select
            v-model="form.onlineStatus"
            placeholder="请选择是否在线 1=在线，2=离线"
          >
            <el-option
              v-for="dict in dict.type.dev_online_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="信号强度" prop="rssi">
          <el-input v-model="form.rssi" placeholder="请输入信号强度" />
        </el-form-item>
        <el-form-item label="信噪比" prop="snr">
          <el-input v-model="form.snr" placeholder="请输入信噪比" />
        </el-form-item>
        <el-form-item label="电池电压单位：伏特" prop="voltage">
          <el-input
            v-model="form.voltage"
            placeholder="请输入电池电压单位：伏特"
          />
        </el-form-item>
        <el-form-item label="距离值单位:厘米" prop="distance">
          <el-input
            v-model="form.distance"
            placeholder="请输入距离值单位:厘米"
          />
        </el-form-item>
        <el-form-item label="信号接收功率" prop="rsrp">
          <el-input v-model="form.rsrp" placeholder="请输入信号接收功率" />
        </el-form-item>
        <el-form-item label="温度" prop="temperature">
          <el-input v-model="form.temperature" placeholder="请输入温度" />
        </el-form-item>
        <el-form-item
          label="信号强度级别(0:信号最强；1；2：信号较差)"
          prop="sign"
        >
          <el-input
            v-model="form.sign"
            placeholder="请输入信号强度级别(0:信号最强；1；2：信号较差)"
          />
        </el-form-item>
        <el-form-item label="满溢状态" prop="full">
          <el-select v-model="form.full" placeholder="请选择满溢状态">
            <el-option
              v-for="dict in dict.type.alarm_yes_or_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="帧计数" prop="frameCount">
          <el-input v-model="form.frameCount" placeholder="请输入帧计数" />
        </el-form-item>
        <el-form-item label="是否火灾报警" prop="fire">
          <el-select v-model="form.fire" placeholder="请选择是否火灾报警">
            <el-option
              v-for="dict in dict.type.alarm_yes_or_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否倾倒" prop="fall">
          <el-select v-model="form.fall" placeholder="请选择是否倾倒">
            <el-option
              v-for="dict in dict.type.alarm_yes_or_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="网络基站 ID" prop="cell">
          <el-input v-model="form.cell" placeholder="请输入网络基站 ID" />
        </el-form-item>
        <el-form-item label="电池状态" prop="batteryQuantityStatus">
          <el-select
            v-model="form.batteryQuantityStatus"
            placeholder="请选择电池状态"
          >
            <el-option
              v-for="dict in dict.type.alarm_yes_or_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角度；设备倾斜的相对角度" prop="angle">
          <el-input
            v-model="form.angle"
            placeholder="请输入角度；设备倾斜的相对角度"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listEventFac,
  getEventFac,
  delEventFac,
  addEventFac,
  updateEventFac,
} from "@/api/alarm/fac";

export default {
  name: "EventFac",
  dicts: ["dev_online_status", "alarm_yes_or_no"],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 满溢设备实时数据表格数据
      facilityRealtimeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceId: undefined,
        dataTime: undefined,
        onlineStatus: undefined,
        rssi: undefined,
        snr: undefined,
        voltage: undefined,
        distance: undefined,
        rsrp: undefined,
        temperature: undefined,
        sign: undefined,
        full: undefined,
        frameCount: undefined,
        fire: undefined,
        fall: undefined,
        cell: undefined,
        batteryQuantityStatus: undefined,
        angle: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        realtimeId: [{ required: true, message: "不能为空", trigger: "blur" }],
        deviceId: [
          { required: true, message: "设备id不能为空", trigger: "blur" },
        ],
        dataTime: [
          { required: true, message: "数据时间不能为空", trigger: "blur" },
        ],
        onlineStatus: [
          {
            required: true,
            message: "是否在线 1=在线，2=离线不能为空",
            trigger: "change",
          },
        ],
        rssi: [
          { required: true, message: "信号强度不能为空", trigger: "blur" },
        ],
        snr: [{ required: true, message: "信噪比不能为空", trigger: "blur" }],
        voltage: [
          {
            required: true,
            message: "电池电压单位：伏特不能为空",
            trigger: "blur",
          },
        ],
        distance: [
          {
            required: true,
            message: "距离值单位:厘米不能为空",
            trigger: "blur",
          },
        ],
        rsrp: [
          { required: true, message: "信号接收功率不能为空", trigger: "blur" },
        ],
        temperature: [
          { required: true, message: "温度不能为空", trigger: "blur" },
        ],
        sign: [
          {
            required: true,
            message: "信号强度级别(0:信号最强；1；2：信号较差)不能为空",
            trigger: "blur",
          },
        ],
        full: [
          { required: true, message: "满溢状态不能为空", trigger: "change" },
        ],
        frameCount: [
          { required: true, message: "帧计数不能为空", trigger: "blur" },
        ],
        fire: [
          {
            required: true,
            message: "是否火灾报警不能为空",
            trigger: "change",
          },
        ],
        fall: [
          { required: true, message: "是否倾倒不能为空", trigger: "change" },
        ],
        cell: [
          { required: true, message: "网络基站 ID不能为空", trigger: "blur" },
        ],
        batteryQuantityStatus: [
          { required: true, message: "电池状态不能为空", trigger: "change" },
        ],
        angle: [
          {
            required: true,
            message: "角度；设备倾斜的相对角度不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询满溢设备实时数据列表 */
    getList() {
      this.loading = true;
      listEventFac(this.queryParams).then((response) => {
        this.facilityRealtimeList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        realtimeId: undefined,
        deviceId: undefined,
        dataTime: undefined,
        onlineStatus: undefined,
        rssi: undefined,
        snr: undefined,
        voltage: undefined,
        distance: undefined,
        rsrp: undefined,
        temperature: undefined,
        sign: undefined,
        full: undefined,
        frameCount: undefined,
        fire: undefined,
        fall: undefined,
        cell: undefined,
        batteryQuantityStatus: undefined,
        angle: undefined,
        createTime: undefined,
        updateTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.realtimeId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加满溢设备实时数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const realtimeId = row.realtimeId || this.ids;
      getEventFac(realtimeId).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改满溢设备实时数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.realtimeId != null) {
            updateEventFac(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addEventFac(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const realtimeIds = row.realtimeId || this.ids;
      this.$modal
        .confirm('是否确认删除"' + (row.orderName || "已勾选") + '"的数据项?')
        .then(() => {
          this.loading = true;
          return delEventFac(realtimeIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "alarm/facilityRealtime/export",
        {
          ...this.queryParams,
        },
        `facilityRealtime_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
